package com.frxuexi.park.view

import android.widget.PopupMenu
import android.widget.SearchView.OnQueryTextListener
import androidx.activity.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.facade.annotation.Route
import com.frxuexi.lib_base.entity.ParkBeanItem
import com.frxuexi.lib_base.http.RequestType
import com.frxuexi.lib_base.mvi.BaseActivity
import com.frxuexi.lib_base.mvi.UiState
import com.frxuexi.lib_base.utils.ArouterManager
import com.frxuexi.park.adapter.ParkAdapter2
import com.frxuexi.park.intent.ParkIntent
import com.frxuexi.park.viewmodel.ParkViewModel
import com.frxuexi.park.R
import com.frxuexi.park.databinding.ActivityParkBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch

@Route(path = "/park/ParkActivity")
@AndroidEntryPoint
class ParkActivity : BaseActivity<ActivityParkBinding, ParkIntent, ParkViewModel>() {
    override val binding: ActivityParkBinding by lazy { ActivityParkBinding.inflate(layoutInflater) }
    override val vm: ParkViewModel by viewModels<ParkViewModel>()
//    val parkAdapter by lazy { ParkAdapter() }

    val parkAdapter2 by lazy { ParkAdapter2() }

    //    lateinit var items:PagingData<ParkBeanItem>
    lateinit var items: Collection<ParkBeanItem>
    var position = -1
    var id = -1
    var type = "类型"

    override fun initData() {
        lifecycleScope.launch {
            vm.uiState.collect {
                when (it) {
                    is UiState.onSuccess<*> -> {
                        when (it.type) {
                            RequestType.DEFAULT -> {
//                                items = it.data as PagingData<ParkBeanItem>
//                                parkAdapter.submitData(lifecycle,items)
                                items = it.data as Collection<ParkBeanItem>
                                parkAdapter2.apply {
                                    data.clear()
                                    addData(items)
                                    notifyDataSetChanged()
                                }
                            }

                            RequestType.DELETE -> {
                                /*parkAdapter.submitData(lifecycle,items.filter {
                                    it.id!=id
                                })*/
//                                parkAdapter.notifyItemRemoved(position)
                                parkAdapter2.notifyItemRemoved(position)
                            }

                            else -> {}
                        }


                    }

                    else -> {}
                }
            }
        }
    }

    override fun initView() {
        binding.parkRv.apply {
            adapter = parkAdapter2
            layoutManager = LinearLayoutManager(this@ParkActivity)
        }

        /*parkAdapter.setOnItemLongClickListener(object :OnItemLongClickListener{
            override fun onItemLongClick(): Boolean {
                ArouterManager.nav(this@ParkActivity,"/park/ParkVRActivity")
                return true
            }
        })*/

        parkAdapter2.setOnItemLongClickListener { adapter, view, position ->
            ArouterManager.nav(this@ParkActivity,"/park/ParkVRActivity")
            return@setOnItemLongClickListener true
        }

        binding.headAdd.setOnClickListener {
            ArouterManager.nav(this, "/park/ParkAddActivity")
        }

        parkAdapter2.setOnItemClickListener { adapter, view, position ->
            val myId = parkAdapter2.data.get(position).id
            ArouterManager.nav(this@ParkActivity,"/park/ParkAddActivity",myId)
        }

        /*parkAdapter.setOnItemClickListener(object :OnItemClickListener{
            override fun onItemClick(id: Int) {
                ArouterManager.nav(this@ParkActivity,"/park/ParkAddActivity",id)
            }
        })*/



        ItemTouchHelper(object :
            ItemTouchHelper.SimpleCallback(ItemTouchHelper.LEFT, ItemTouchHelper.LEFT) {
            override fun onMove(
                recyclerView: RecyclerView,
                viewHolder: RecyclerView.ViewHolder,
                target: RecyclerView.ViewHolder
            ): Boolean {
                return false
            }

            override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
                position = viewHolder.adapterPosition
//                id = parkAdapter.snapshot().items.get(position).id!!

                vm.sendIntent(ParkIntent.deletePark(parkAdapter2.data.get(position).id!!))
            }
        }).attachToRecyclerView(binding.parkRv)

        binding.parkSv.setOnQueryTextListener(object : OnQueryTextListener {
            override fun onQueryTextSubmit(query: String?): Boolean {
                if (type == "车牌号") {
                    lifecycleScope.launch {
                        /*parkAdapter.submitData(
                            items.filter { it.licensePlate==query!! }
                        )*/
                        parkAdapter2.data.clear()
                        parkAdapter2.addData(items.filter { it.licensePlate==query!! })
                        parkAdapter2.notifyDataSetChanged()
                    }
                } else if (type == "类型") {
                    vm.sendIntent(ParkIntent.getParksByType(query!!, type))
                } else {
                    vm.sendIntent(ParkIntent.getParksByArea(query!!, type))
                }
                return true
            }

            override fun onQueryTextChange(newText: String?): Boolean {
                if (newText.isNullOrEmpty()) {
                    if (type == "车牌号") {
                        lifecycleScope.launch {
//                            parkAdapter.submitData(items)
                            parkAdapter2.addData(items)
                        }
                    } else {
                        vm.sendIntent(ParkIntent.getParksByType("标准车位", "类型"))
                    }
                }
                return true
            }
        })

        binding.parkFilter.setOnClickListener {
            val popupMenu = PopupMenu(this, it)
            popupMenu.inflate(R.menu.menu_park)

            popupMenu.setOnMenuItemClickListener {
                when (it.itemId) {
                    R.id.filter_type -> {
                        type = "类型"
                    }

                    R.id.filter_carNo -> {//根据车牌号
                        type = "车牌号"
                    }

                    R.id.filter_area -> {
                        type = "区域"
                    }

                    else -> {//根据状态
                        vm.sendIntent(ParkIntent.getParksByStatus(it.title.toString(), "状态"))
                    }
                }


                true
            }
            popupMenu.show()
        }


    }

    override fun onResume() {
        super.onResume()
        vm.sendIntent(ParkIntent.getParksByType("标准车位", "类型"))
    }

}